/*
 * Copyright 1999-2010 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package com.zb.zookeeper.zookeeper.example;

import org.apache.zookeeper.*;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 类AsynCreate.java的实现描述：TODO 类实现描述
 *
 * @author zookeeper Jan 2, 2014 9:39:02 PM
 */
public class AsynCreate {

    private static LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();
    private static ZooKeeper zookeeper;
    private static String node = "/dubbo";
    private static List<String> childrenList;

    /**
     * @param args
     * @throws IOException
     * @throws InterruptedException
     */
    public static void main(String[] args) throws IOException, InterruptedException {
        zookeeper = new ZooKeeper("10.201.7.161:2181", 200000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                try {
                    if (event.getType() == Event.EventType.NodeChildrenChanged && node.equals(event.getPath())) {
                        updateServerList();
                        queue.put("State: " + event.getState() + "--Type: " + event.getType() + "--Path: " + event.getPath() + "--Wrapper: " + event.getWrapper());
                    }
                    updateServerList();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        try {
            childrenList = zookeeper.getChildren(node, true);
            childrenList.forEach(System.out::println);
        } catch (KeeperException | InterruptedException e) {
            System.out.println("======KeeperException======" + e);
        }


        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    String t = queue.poll();
                    if (t != null)
                        System.out.println(t);
                }
            }
        }).start();

    }

    private static void updateServerList() throws Exception {
        List<String> subNodeList = zookeeper.getChildren(node, true);
        subNodeList.stream().filter(o -> !childrenList.contains(o)).forEach(System.out::println);
    }

    public static void handle() throws InterruptedException {
        Thread.sleep(1000 * 60 * 2);
    }

    /**
     * 关闭ZK连接
     */
    public void releaseConnection() {
        if (zookeeper != null) {
            try {
                zookeeper.close();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


}













/*
        zookeeper.create("/dubbo", "sid-o2".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL,
                         new StringCallback() {

            @Override
            public void processResult(int rc, String path, Object ctx, String name) {
                Code code = Code.get(rc);
                switch (code) {
                    case OK:
                        System.out.println(code);
                        break;
                    case NODEEXISTS:
                        System.out.println(code);
                        break;
                    case SESSIONEXPIRED:
                        System.out.println(code);
                        break;
                    default:
                        System.out.println("unknow " + code);
                }

            }
        }, null);

        DataCallback callback = new DataCallback() {

            @Override
            public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
                Code code = Code.get(rc);
                System.out.println("code for check " + code);
                switch (code) {
                    case OK:
                        break;
                    case NONODE:
                        break;
                    case NODEEXISTS:
                        break;
                    case SESSIONEXPIRED:
                        break;
                    default:
                }

            }

        };

        zookeeper.getData(node, true, callback, null);
*/